// pages/Goods/Comment/index.js
const common = require('../../../public.js')
const utils = require('../utils.js')
const config = require('../../../config')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    commentList:[],
    isFyApp: config.isFyApp,
    storeId: '',
    commnet: [],
    commentTotalCount:'',
    showComment: false,
    commentValue: '',
    pageNum: 1,
    pageSize: 10,
    isLoadingBusy: false,
    loadFinished: false,
    active: 3,
    userInfo: null,
    textareaForce: false,
    pubUserId: '',
    commentId: '',
    commentPlachold: '我来说几句',
    focus: false,
    articleDetail: {},
    refreshing:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    this.data.storeId = options.id ? options.id : config.id
    this.data.moduleType = options.moduleType ? options.moduleType : config.type
    this.setData({
      isLoadingBusy:true
    })
    common.getLocalUserInfo(function (success, data) {
      if (success) {
        that.data.token = data.token
        that.data.openid = data.openid
        that.setData({
          userInfo: data.user
        }, function () {
          // 获取商品评论
          that.getCommentList(true)
        })
      }
    }, false)
  },
  textareaBindblur: function () {
    this.setData({
      textareaForce: false,
      commentPlachold: '我来说几句'
    })
  },
  textareaBindfocus: function () {
    console.log('textareaBindfocus---')
    this.setData({
      textareaForce: true,
    })
  },
  onChangeTabbar(e) { //切换底部导航
    const index = e.detail.index
    const url = e.detail.url
    if (index != this.data.active) {
      utils.onChangeTabbar(url, this.data.storeId, this.data.moduleType)
    }
  },
  //获取评论列表
  getCommentList(refresh = false) {
    if (!refresh && (this.data.isLoadingBusy || this.data.noMoreData)) {
      return;
    }
    this.setData({
      isLoadingBusy: true,
      refreshing: refresh
    })
    const pageNum = refresh ? 1 : this.data.pageNum + 1

    const that = this
    const tempJson = {
      storeId: that.data.storeId,
      pageNum,
      pageSize: that.data.pageSize,
      userId: that.data.userInfo.id,
      token: that.data.token
    }
    common.post('/scenicQanswer/getScenicQanswerListForUser', tempJson, function (res) {
      if (res.status === 200) {
        console.log('啊啊啊-',res);
        that.data.pageNum = pageNum
        if(refresh){// 下拉刷新
          that.setData({
            commentList: res.data.qaList,
          })
        }else{// 上拉加载
          if (res.data.qaList.length) {// 有值
            that.setData({
              commentTotalCount:res.data.qaCount,
              commentList: [...that.data.commentList, ...res.data.qaList],
            })
          }else{
            that.setData({
              commentTotalCount:res.data.qaCount,
              noMoreData: true,
            })
          }
        }
 
      } else {
        that.setData({
          noMoreData: true
        })
      }
    }, function () {
      console.log('嘻嘻嘻嘻哈哈哈');
      that.setData({
        isLoadingBusy: false,
        refreshing: false
      })
    })
  },
  loadMore() {
    this.getCommentList(false)
  },
  commentSave() {
    if (common.isEmptyString(this.data.commentValue)) {
      common.showToast('您没有输入任何内容')
      return
    }
    console.log('commentIdwei',this.data.commentId);
    if (this.data.commentId) { // 有commentid是二级评论
      this.replyComment()
      console.log('回复')
  } else {
      this.commentFunc()
      console.log('评论---')
  }


    // TODO
  },
  // //点击回复
  // clickRely(e) {
  //   let {
  //     item
  //   } = e.currentTarget.dataset
  //   this.setData({
  //     focus: true,
  //     commentPlachold: '@' + item.alias + ':',
  //     commentId: item.id
  //   })
  // },
  clickReply(e) { // 点击评论中的回复
    let that = this
    console.log('回复Ta--',e);
    let {
        pubuserid,
        commentid,
        pubusername
    } = e.currentTarget.dataset
    that.setData({
        pubUserId: pubuserid,
        commentId: commentid,
        commentPlachold: '@' + pubusername + '：'
    })
},
onChange(e) {
  let {
      value
  } = e.detail

  this.setData({
      commentValue: value
  })
},

commentFunc() { 
  const that = this
  const tempJson = {
    token: that.data.token,
      userId: that.data.userInfo.id,
    storeId: that.data.storeId,
    content: that.data.commentValue
  }
  common.post('/scenicQanswer/saveScenicQanswer', tempJson,
    function (res) {
      if (res.status === 200) {
        that.clearReplyInfo()
        that.getCommentList(true)
      } else {
        common.showRequestErrMsg(res)
      }
    },
    function () {
      that.setData({
        isLoadingBusy: false
      })
    })
},

replyComment() { // 初始化店铺信息
  const that = this
  const tempJson = {
    token: that.data.token,
      userId: that.data.userInfo.id,
    qanswerId: that.data.commentId,// 问答ID
    pubUserId: that.data.userInfo.id,// 回复人ID
    receiverUserId: that.data.pubUserId,// 被回复人ID
    content: that.data.commentValue
  }
  common.post('/scenicQanswer/saveScenicQanswerChat', tempJson,
    function (res) {
      if (res.status === 200) {
        that.clearReplyInfo()
        that.getCommentList(true)
      } else {
        common.showRequestErrMsg(res)
      }
    },
    function () {
      that.setData({
        isLoadingBusy: false
      })
    })
},

clearReplyInfo() {
  this.setData({
      commentValue: '',
      pubUserId: "",
      commentId: '',
      commentPlachold: '我来说几句'
  })
},
praiseAction(e) {
  const id = e.currentTarget.dataset.item.id
  const userPraiseFlag = e.currentTarget.dataset.item.userPraiseFlag
  if (userPraiseFlag) {
    console.log('取消点赞')
      this.cancleCommentPraise(id)
  } else {
    console.log('点赞')
      this.commitPraise(id)
  }
},

cancleCommentPraise(id) {
  let that = this
  let tempJson = {
      userId: (that.data.userInfo && that.data.userInfo.id) ? that.data.userInfo.id : '',
      token: that.data.token,
      qanswerId: id
  }
  common.post('/scenicQanswer/cancleScenicQanswerPraise', tempJson, function (res) {
      if (res.status === 200) {
        that.getCommentList(true)
      }
  })
},
commitPraise(id) { // 点赞
  const that = this
  let tempJson = {
      userId: that.data.userInfo.id,
      token: that.data.token,
      qanswerId: id
  }
  common.post('/scenicQanswer/saveScenicQanswerPraise', tempJson, function (res) {
      if (res.status == 200) {
        that.getCommentList(true)
      }
  })
},

 //获取全部列表
 getAllComment() {
  wx.navigateTo({
      url: '/pages/ScenicArea/ScenicAllQuestion/index?itemId=' + this.data.articleId,
  })
},
onPullDownRefresh() {
  console.log('下拉刷新')
  this.getCommentList(true);//需要再次调用接口的函数
  
  },
  onReachBottom() {
    console.log('上拉加载')
    this.getCommentList(false)
  }
})